package com.ydj.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ydj.app.entity.CmsAccount;
import com.ydj.app.protocol.http.cms.*;
import com.ydj.app.service.BannerService;
import com.ydj.framework.annotation.AllowGuest;
import com.ydj.framework.annotation.Idempotence;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.protocol.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/cms/banner")
@Api(tags = "CMS-Banner")
public class CmsBannerController {

    @Autowired
    private BannerService bannerService;

    @ResponseBody
    @ApiOperation(value = "列表")
    @ApiOperationSupport(order = 10)
    @PostMapping(value = "/list")
    public JsonResult<CmsBannerListRes> list(@RequestBody @Validated CmsBannerListReq req) throws CommonException {
        CmsBannerListRes res = bannerService.cmsBannerList(req);
        return JsonResult.success(res);
    }

    @ResponseBody
    @ApiOperation(value = "新增")
    @ApiOperationSupport(order = 20)
    @PostMapping(value = "/create")
    public JsonResult<?> create(@RequestBody @Validated CmsBannerCreateReq req) throws CommonException {
        bannerService.cmsBannerCreate(req);
        return JsonResult.success();
    }

    @ResponseBody
    @ApiOperation(value = "修改")
    @ApiOperationSupport(order = 30)
    @PostMapping(value = "/update")
    public JsonResult<?> update(@RequestBody @Validated CmsBannerUpdateReq req) throws CommonException {
        bannerService.cmsBannerUpdate(req);
        return JsonResult.success();
    }

    @ResponseBody
    @ApiOperation(value = "删除")
    @ApiOperationSupport(order = 40)
    @PostMapping(value = "/delete")
    public JsonResult<?> delete(@RequestBody @Validated CmsBannerDeleteReq req) throws CommonException {
        bannerService.cmsBannerDelete(req);
        return JsonResult.success();
    }

    @Idempotence
    @ResponseBody
    @ApiOperation(value = "调整轮播图顺序")
    @ApiOperationSupport(order = 15)
    @PostMapping(value = "/adjustOrder")
    public JsonResult<?> adjustOrder(@RequestBody @Validated CmsBannerAdjustOrderReq req, @ApiIgnore CmsAccount account) throws CommonException {
        bannerService.cmsBannerAdjustOrder(req, account);
        return JsonResult.success();
    }

}